module game {
    class BattleHandle2 {
        battleInfo: any
        battleView:view.BattleView2
        attackerInfo: any
        defenderInfo: any
        timer: egret.Timer
        public constructor() {
            this.battleInfo = {
                playerInfo1: null,
                playerInfo2: null
            }
        }
        //设置基本信息
        public setBaseInfo(player1,player2,hpPB1,hpPB2,battleView: view.BattleView2) {
            this.battleInfo.playerInfo1 = {
                fighter: player1.fightAttri,
                skillRelease: [],
                state1: 0, //0无状态1cd中
                state2: 0, //0无状态1被控制中
                hpPB: hpPB1,
            }
            this.battleInfo.playerInfo2 = {
                fighter: player2.fightAttri,
                skillRelease: [],
                state1: 0, //0无状态1cd中
                state2: 0, //0无状态1被控制中
                hpPB: hpPB2,
            }
            this.battleView = battleView
            this.attackerInfo = this.battleInfo.playerInfo1
            this.defenderInfo = this.battleInfo.playerInfo2
        }
        //开始
        public start() {
            var timer: egret.Timer = new egret.Timer(800,0);
            timer.addEventListener(egret.TimerEvent.TIMER,this.startBattle,this);
            timer.start()
            this.timer = timer
                
        }
        
        //战斗
        private startBattle(){
            this.attack(this.attackerInfo,this.defenderInfo)
            var temp = this.defenderInfo
            this.defenderInfo = this.attackerInfo
            this.attackerInfo = temp
        }

        //攻击
        private attack(attackerInfo,defenderInfo) {
            var self = this
            //var skillId = this.skillRate(gplayer.fightAttri.skills)
            var skillId = 120001
            var damage = skillHandle[skillId](attackerInfo.fighter,defenderInfo.fighter)
            defenderInfo.fighter.hp -= damage
            defenderInfo.hpPB.value = defenderInfo.fighter.hp
            if(defenderInfo.fighter.hp <= 0) {
                this.timer.stop()
                this.battleView.close()
                return
            }
        }
        
        public skillRate(skills):number{
            var totalRate = 0
            for(var i in skills){
                var skillId = skills[i]
                var skillConfig = totalConfig[skillId]
                totalRate += skillConfig.skillRate
            }
            var rate = Math.ceil(Math.random() * totalRate)
            for(var i in skills) {
                var skillId = skills[i]
                var skillConfig = totalConfig[skillId]
                if(rate <= skillConfig.skillRate){
                    return skillId
                }
                rate -= skillConfig.skillRate
            }
        }
        
        //计算伤害
        public calcDamage(attacker: FightAttri,defender: FightAttri):number {
            var damage: number = attacker.attack * attacker.attack / (defender.defend + attacker.attack) //计算伤害
            if(damage <= 0) {
                damage = 1
            }
            damage = Math.ceil(damage)
            return damage
        }
    }
    export var battleHandle2 = new BattleHandle2()
}
